package com.fandy.struts.action;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.DispatchAction;

import com.fandy.struts.DAL.DALGoodsInfo;
import com.fandy.struts.Meta.GoodsInfo;
import com.fandy.struts.form.FormFileUpLoad;
import com.fandy.struts.util.*;

public class ActionFileUpLoad extends DispatchAction {

	private static int MaxSize = 1024000;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.struts.actions.DispatchAction#execute(org.apache.struts.action
	 * .ActionMapping, org.apache.struts.action.ActionForm,
	 * javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub

		FormFileUpLoad fful = (FormFileUpLoad) form;
		ActionForward af = null;
		ActionErrors ae = new ActionErrors();
		ResultSet rs = null;
		com.fandy.struts.Model.GoodsInfo gi = new com.fandy.struts.Model.GoodsInfo();
		
		
		String fileType = fful.getMyfile().getContentType();
		int sz = fful.getMyfile().getFileSize();

		System.out.println(fileType.equals("image/jpeg"));
		
		if ((!fileType.equals("image/jpeg") && !fileType.equals("image/gif"))
				|| sz > MaxSize) {
			ae.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
					"Error.FileUpLoad.Type"));
			ae.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
					"Error.FileUpLoad.Size"));
			
			saveErrors(request, ae);
			
			af = mapping.findForward(MappingForward.Failure);
			
			 
			
			return af;
		}

		byte[] content = fful.getMyfile().getFileData();

		DALGoodsInfo dgi = new DALGoodsInfo();
			//保存新图片到磁盘
		String Name = FileOperator.SaveBytesToFile(content, fileType.replace('/', '.'), request
				.getRealPath("/"));

		rs = dgi.GetGoodsByID(fful.getGoodsid());
		
		System.out.println(Name);
		System.out.println(request.getRealPath("/"));
		
		
		
		rs.next();
		SetGI(gi, rs);
		
		//删除原图片
		FileOperator.DeleteFile(gi.getImage(), request
				.getRealPath("/"));

		//更新数据库
		gi.setImage(Name.replace('\\', '/'));
		dgi.UpdatebyID(fful.getGoodsid(),gi );
		
		
		
		
		af = mapping.findForward(MappingForward.Success);
		
		 
		
		return af;
	}
	
	public com.fandy.struts.Model.GoodsInfo  SetGI(
			com.fandy.struts.Model.GoodsInfo gInfo, ResultSet rs)
	throws SQLException {
gInfo.setBrand(rs.getString(GoodsInfo.getBrand()));
gInfo.setGoodsid(rs.getInt(GoodsInfo.getGoodsid()));
gInfo.setImage(rs.getString(GoodsInfo.getImage()));
gInfo.setOrigin(rs.getString(GoodsInfo.getOrigin()));
gInfo.setProfit(rs.getFloat(GoodsInfo.getProfit()));
gInfo.setSize(rs.getString(GoodsInfo.getSize()));
gInfo.setType(rs.getString(GoodsInfo.getType()));
gInfo.setUnit(rs.getString(GoodsInfo.getUnit()));

return gInfo;
}
	
	

}
